package it.bd.SetAllConnectionsOff;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;

import it.bd.SetAllConnectionsOff.activity.AdvancedTabActivity;
import it.bd.SetAllConnectionsOff.helper.StateHelper;

public class ScreenListenerService extends Service {
	
	private SharedPreferences	prefs;
	
	@Override
	public void onCreate() {
		super.onCreate();
		// register receiver that handles screen on and screen off logic
		IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
		filter.addAction(Intent.ACTION_SCREEN_OFF);
		BroadcastReceiver mReceiver = new ScreenReceiver();
		registerReceiver(mReceiver, filter);
		prefs = getSharedPreferences(SetAllConnectionsOff.PREF_NAME, MODE_PRIVATE);
	}
	
	@Override
	public void onStart(Intent intent, int startId) {
		if (prefs.getBoolean(AdvancedTabActivity.SCREENONOFF_PREF_NAME, false)) {
			if (intent.hasExtra("screen_state")) {
				boolean screenOff = intent.getBooleanExtra("screen_state", false);
				StateHelper.getInstance().setState(screenOff);
				// NetworkHelper.getInstance().setServiceEnabled(!screenOff);
				
			}
		}
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
